<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\user;

use App\Dao\GoodsCategoryDao;
use App\Dao\GoodsDao;
use App\Dao\GoodsSkuDao;
use App\Dao\UserFavDao;
use App\Job\IntegralJob;
use App\Request\User\Goods\SearchRequest;
use App\Request\User\Goods\SkuDetailRequest;
use App\Middleware\User\UserNotNecessaryMiddleware;

use App\Service\ConfigService;
use App\Service\User\GoodsService;
use App\Service\User\IntegralDetailService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;

/**
 * 商城服务
 *
 * @AutoController()
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\user
 */
class GoodsController extends Controller
{
    /**
     * 商品 Dao
     *
     * @Inject
     * @var GoodsDao
     */
    private $dao;

    /**
     * 商品分类 Dao
     *
     * @Inject
     * @var GoodsCategoryDao
     */
    private $goods_category_dao;

    /**
     * Sku Dao
     *
     * @Inject
     * @var GoodsSkuDao
     */
    private $sku_dao;

    /**
     * @Inject
     * @var UserFavDao
     */
    private $user_fav_dao;

    /**
     * @Inject
     * @var GoodsService
     */
    private $goods_service;

    /**
     * @Inject
     * @var ConfigService
     */
    private $config_service;
    /**
     * @Inject
     * @var IntegralDetailService
     */
    private $integral_detail_service;

    /**
     * 商品列表
     */
    public function get()
    {
        $category_id = (int)$this->request->input('category_id', 0);

        // 商品列表
        $page_goods_list = $this->dao->getGoodsListByCategoryId($category_id);

        $this->success($page_goods_list);
    }

    /**
     * 商品详情
     *
     * @Middlewares({
     *     @Middleware(UserNotNecessaryMiddleware::class)
     * })
     */
    public function detail()
    {
        $goods_id = (int)$this->request->input('id', 0);

        // 商品信息
        $goods = $this->dao->first($goods_id);

        $goods['property'] = $this->goods_service->goods_sku( $goods['sku'] );

        $goods['user_integral'] = $this->integral_detail_service->getIntegralBalance(uid());

        preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/",$goods['contents']['content'],$new_array);
        // 隐藏重要的字段
        $goods->makeHidden(['sku', 'category_id', 'disable', 'created_at', 'updated_at', 'deleted_at']);

        $goods['contents']['content'] = $new_array[1];
        // 商品收藏状态
        $user_id = uid(false);
        if ($user_id !== null) {
            $goods->fav_status = (int)$this->user_fav_dao->getGoodsFavStatus($user_id, $goods_id);
        } else {
            $goods->fav_status = 0;
        }

        $this->success($goods);
    }

    /** 
     * SKU 详情接口
     *
     * @param SkuDetailRequest $request
     */
    public function sku_detail(SkuDetailRequest $request)
    {
        $goods_id = (int)$request->input('goods_id');

        $properties = (string)$request->input('properties');

        $result = $this->sku_dao->getDetailByProperties($goods_id, $properties);

        $this->success($result);
    }

    /**
     * 分类列表
     */
    public function category_list()
    {
        $category_list = $this->goods_category_dao->getGoodsCategoryList();

        // 创建 tree 树
        $category_list = createTree($category_list->toArray(), 0, ['id', 'pid', 'name', 'icon', 'children'], 'children');

        $this->success($category_list);
    }

    /**
     * 搜索
     *
     * @param SearchRequest $request
     */
    public function search(SearchRequest $request)
    {
        $params = $request->all();

        $params['sort_rule'] = isset($params['sort_rule']) ? (int)$params['sort_rule'] : 0;

        // 排序规则列表
        $sort_rule_list = [
            '0' => ['id', 'asc'],
            '1' => ['price', 'desc'],
            '2' => ['price', 'asc'],
            '3' => ['sales', 'desc'],
            '4' => ['sales', 'asc'],
        ];
        $sort_rule = $sort_rule_list[$params['sort_rule']];

        // 搜索商品
        $page_goods_list = $this->dao->search($params['title'], $sort_rule);

        $this->success($page_goods_list);
    }
}